//
//  STBaseNavigationViewController.m
//  STBasicsApp
//
//  Created by jerry on 2019/5/1.
//  Copyright © 2019 jerry. All rights reserved.
//

#import "STBaseNavigationViewController.h"

@interface STBaseNavigationViewController ()


@end

@implementation STBaseNavigationViewController

+ (void)initialize
{
    //UINavigationBar *bar = [UINavigationBar appearance];
    //bar.barTintColor = kRedColor;
    //bar.dk_barTintColorPicker = DKColorPickerWithKey(BG);
    //bar.dk_barTintColorPicker = DKColorPickerWithRGB(0xffffff, 0x343434);
//    [bar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17.0f],
//                                  NSForegroundColorAttributeName:kWhiteColor}];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置主题颜色
    self.navigationBar.dk_barTintColorPicker = DKColorPickerWithKey(NAVIGATIONBG);
    
    //self.navigationBar.dk_tintColorPicker = DKColorPickerWithKey(NAVIGATIONTEXT);
    
    if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
        [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f],
                                                     NSForegroundColorAttributeName:UIColorFromRGB(THEME_KEY_NIGHT_COLOR_NAVTEXT)}];
    }else if ([self.dk_manager.themeVersion isEqualToString:THEME_KEY_RED]) {
        [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f],
                                                     NSForegroundColorAttributeName:UIColorFromRGB(THEME_KEY_RED_COLOR_NAVTEXT)}];
    }else if ([self.dk_manager.themeVersion isEqualToString:THEME_KEY_GREEN]) {
        [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f],
                                                     NSForegroundColorAttributeName:UIColorFromRGB(THEME_KEY_GREEN_COLOR_NAVTEXT)}];
    }else if ([self.dk_manager.themeVersion isEqualToString:THEME_KEY_YELLOW]) {
        [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f],
                                                     NSForegroundColorAttributeName:UIColorFromRGB(THEME_KEY_YELLOW_COLOR_NAVTEXT)}];
    }else{
        [self.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:18.0f],
                                                     NSForegroundColorAttributeName:UIColorFromRGB(THEME_KEY_NORMAL_COLOR_NAVTEXT)}];
    }
    
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if (self.childViewControllers.count) { // 隐藏导航栏
        viewController.hidesBottomBarWhenPushed = YES;
        
        // 自定义返回按钮
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitleColor:kWhiteColor forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithRed:0.000 green:0.613 blue:0.515 alpha:1.000] forState:UIControlStateHighlighted];
        
        if ([self.dk_manager.themeVersion isEqualToString:DKThemeVersionNight]) {
            [btn setImage:[UIImage imageNamed:@"btn_nav_white_n"] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"btn_nav_white_p"] forState:UIControlStateHighlighted];
        } else {
            [btn setImage:[UIImage imageNamed:@"btn_nav_back_n"] forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"btn_nav_back_p"] forState:UIControlStateHighlighted];
        }
      
        [btn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [btn sizeToFit];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
        
        // 如果自定义返回按钮后, 滑动返回可能失效, 需要添加下面的代码
        __weak typeof(viewController)Weakself = viewController;
        self.interactivePopGestureRecognizer.delegate = (id)Weakself;
        
    }
    
    [super pushViewController:viewController animated:animated];
}

- (void)back
{
    // 判断两种情况: push 和 present
    if ((self.presentedViewController || self.presentingViewController) && self.childViewControllers.count == 1) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else
        [self popViewControllerAnimated:YES];
}

-(UIViewController *)childViewControllerForStatusBarStyle {
    return self.topViewController;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
